<!--用户管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;float: left">招聘信息</span>
<!--    <el-button @click="dialogVisible=true" type="primary" color="#5B63B7"-->
<!--               style="float:right;margin-top:13px;">新建用户</el-button>-->
  </div>
  <!-- 新增简历弹窗 -->
  <el-dialog :title="dialogTitle" style="width:600px;padding:40px;" v-model="dialogVisible" :before-close="handleClose">
    <el-form label-width="50px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="公司名">
            <el-input placeholder="请输入公司名" v-model="savePositionForm.corporateName" :disabled="savePositionForm.corporateName!=null"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="薪资">
            <el-input placeholder="请输入薪资" v-model="savePositionForm.salary"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="福利">
            <el-input placeholder="请输入福利" v-model="savePositionForm.welfare"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="求职类型">
            <el-select placeholder="请选择求职类型" v-model="savePositionForm.jobType">
              <el-option v-for="item in levelOptions" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="岗位详情">
            <el-input placeholder="请输入岗位详情" v-model="savePositionForm.jobDetail"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="行业分类">
            <el-input placeholder="请输入行业分类" v-model="savePositionForm.industryClassification"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="学历要求">
            <el-select placeholder="请选择学历要求" v-model="savePositionForm.jobType">
              <el-option v-for="item in OptionStatus" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="招聘人数">
            <el-input placeholder="请输入招聘人数" v-model="savePositionForm.numberOfRecruits"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="实习天数">
            <el-input placeholder="请输入实习天数" v-model="savePositionForm.weekDay"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实习月份数量">
            <el-input placeholder="请输入实习月份数量" v-model="savePositionForm.internshipMonthDuration"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="招聘城市">
            <el-input placeholder="请输入招聘城市" v-model="savePositionForm.recruitmentCity"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="转正机会">
            <el-input placeholder="请输入转正机会" v-model="savePositionForm.employmentOpportunities"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button  @click="handleClose()" style="color: #5B63B7;" class="custom-hover-color">取消</el-button>
      <el-button type="primary" color="#5B63B7" @click="savePosition()">保存</el-button>
    </template>
  </el-dialog>

  <!-- 查询卡片 -->
  <el-card style="margin:20px;height: 120px;">
    <el-form :inline="true">
      <el-form-item label="行业分类" style="float: left">
        <el-input placeholder="输入行业分类" style="width:220px;"
                  v-model="searchPositionForm.industryClassification" @keydown.enter="loadPosition"></el-input>
      </el-form-item>
      <el-form-item label="岗位详情" style="float: left">
        <el-input placeholder="输入岗位详情" style="width:220px;"
                  v-model="searchPositionForm.jobDetail" @keydown.enter="loadPosition"></el-input>
      </el-form-item>
      <el-form-item label="招聘城市" style="float: left">
        <el-input placeholder="输入招聘城市" style="width:220px;"
                  v-model="searchPositionForm.recruitmentCity" @keydown.enter="loadPosition"></el-input>
      </el-form-item>
      <el-form-item label="求职类型" style="width:290px;float: left">
        <el-select placeholder="请选择求职类型"
                   v-model="searchPositionForm.jobType" @change="loadPosition">
          <el-option v-for="item in levelOptions" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学历要求" style="width:290px;float: left">
        <el-select placeholder="请选择学历要求"
                   v-model="searchPositionForm.educationalRequirements" @change="loadPosition">
          <el-option v-for="item in OptionStatus" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item style="float: left;">
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadPosition" color="#5B63B7">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 用户信息表格 -->
  <el-card style="margin:20px;">
    <el-table :data="positionArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="corporateName" label="公司名" align="center"/>
      <el-table-column prop="salary" label="薪资" align="center"/>
      <el-table-column prop="welfare" label="福利" align="center"/>
      <el-table-column prop="jobType" label="求职类型" align="center"/>
      <el-table-column prop="jobDetail" label="岗位详情" align="center"/>
      <el-table-column prop="industryClassification" label="行业分类" align="center"/>
      <el-table-column prop="educationalRequirements" label="学历要求" align="center" :formatter="educational"/>
      <el-table-column prop="numberOfRecruits" label="招聘人数" align="center"/>
      <el-table-column prop="releaseTime" label="发布时间" align="center"/>
      <el-table-column prop="weekDay" label="每个星期实习天数" align="center"/>
      <el-table-column prop="internshipMonthDuration" label="实习月份数量" align="center"/>
      <el-table-column prop="recruitmentCity" label="招聘城市" align="center"/>
      <el-table-column prop="employmentOpportunities" label="转正机会" align="center"/>

      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="editPosition(scope.row.id)" style="color:#5B63B7">编辑</el-button>
          <el-button link type="primary" size="small" @click="deletePosition(scope.row.id)" style="color:#5B63B7">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import {ElMessage} from "element-plus";
import qs from "qs";

//控制新增用户弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('编辑招聘信息');
//用户职业
const levelOptions = ref([
  {label: '实习',value: '实习'},
  {label: '校招',value: '校招'},
  {label: '全职',value: '全职'},
])
//用户状态
const OptionStatus = ref([
  {label: '大专',value: '10'},
  {label: '本科',value: '20'},
  {label: '研究生',value: '30'},
])
const educational = (row, column,cellValue,index) => {
  if (cellValue==10){
    cellValue= '大专';
  }else if (cellValue==20){
    cellValue= '本科';
  }else if (cellValue==30){
    cellValue= '研究生';
  }
  return cellValue;
}
//准备对象用来收集新增/修改员工的表单信息
const savePositionForm = ref({
  id:'',
  companyId: '',
  corporateName:'',
  salary:'',
  welfare:'',
  jobType: '',
  jobDetail:'',
  industryClassification:'',
  educationalRequirements: '',
  numberOfRecruits:'',
  releaseTime:'',
  weekDay:'',
  internshipMonthDuration:'',
  recruitmentCity:'',
  employmentOpportunities:''
});

//修改招聘信息
const savePosition = () => {
  let data = JSON.stringify(savePositionForm.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/position/save',data, {
    headers: {
      'Content-Type': 'application/json' // 明确指定请求体类型为 JSON
    }
  }).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('保存成功!');
      dialogVisible.value = false;//关闭弹窗
      savePositionForm.value={};//清空输入的数据,防止新增成功后打开弹窗还是之前的数据
      loadPosition();//加载用户列表方法
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//实现取消按钮的功能
const handleClose = ()=>{
  if(confirm('是否关闭本窗口?')){
    //用户确认关闭后,将审批弹窗与确认驳回弹窗一起关掉
    dialogVisible.value = false;
    //取消时把填的数据都清空，表单回复到初始状态
    savePositionForm.value={};
  }
}

//定义数组用来装用户列表信息
const positionArr = ref([]);
//定义对象保存查询条件
const searchPositionForm = ref({jobType: '', industryClassification: '',educationalRequirements:'',recruitmentCity:'',jobDetail:''});
const loadPosition = () => {
  //如果传入了查询条件，则根据条件查询，没有条件，默认查询所有员工信息
  let data = qs.stringify(searchPositionForm.value);
  console.log(data);
  axios.get(BASE_URL+'/v1/position/select?'+data).then((response) => {
    if (response.data.code === 2000) {
      positionArr.value = response.data.data;
      console.log(positionArr.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//页面一加载立即调用
onMounted(() => {
  searchPositionForm.value={};
  loadPosition();//加载员工列表
})

//重置用户搜索信息
const resetSearch = ()=>{
  searchPositionForm.value={};
  loadPosition();
}


//编辑员工
const editPosition = (id) =>{
  console.log(id);
  //修改弹窗标题并打开弹窗
  dialogTitle.value = '编辑招聘信息';
  dialogVisible.value = true;
  //根据id查询要修改的当前员工的信息
  axios.get(BASE_URL+'/v1/position/select?id='+id).then((response) => {
    if (response.data.code === 2000) {
      //若请求成功，赋值给saveUserForm，因为后端返回的是list，所以这里要data[0]
      savePositionForm.value = response.data.data[0];
      console.log(response.data.data)
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//删除用户
const deletePosition = (id)=>{
  console.log(id);
  if(confirm('您确认要删除此数据吗?')){
    axios.post(BASE_URL+'/v1/position/delete/'+id).then((response) => {
      if (response.data.code === 2000) {
        ElMessage.success('删除成功!');
        loadPosition();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>

<style scoped>
.custom-hover-color:hover {
  color: #5B63B7; /* 悬停时的文本颜色 */
  background-color: #f5f5f5;
  border-color: #cccccc;
}
/*出生日期*/
.demo-datetime-picker {
  display: flex;
  width: 100%;
  margin: 0;
  padding: 0;
  flex-wrap: wrap;
  justify-content: space-around;
  align-items: stretch;
}
.demo-datetime-picker .block {
  padding: 30px 0;
  text-align: center;
}
</style>